package cn.jwcat.controller;

import cn.jwcat.dealEntity.DealHealth;
import cn.jwcat.dealEntity.HealthDto;
import cn.jwcat.entity.Health;
import cn.jwcat.entity.Staff;
import cn.jwcat.service.IHealthService;
import cn.jwcat.utils.JwtTokenUtil;
import cn.jwcat.utils.result.Result;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiOperation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 健康状态操作
 */
@Transactional
@RestController
@RequestMapping("/health")
public class HealthController {
    @Resource
    IHealthService healthService;

    @Resource
    JwtTokenUtil jwtTokenUtil;

    @GetMapping("/{page}/{size}")
    @ApiOperation("获取账号状态激活的志愿者的健康信息")
    public Result getHealthInfoByStatus(@PathVariable("page") int page, @PathVariable("size") int size) {
        PageHelper.startPage(page, size);
        List<HealthDto> healthDtos = healthService.getHealthInfoByStatus();
        PageInfo<HealthDto> info = new PageInfo<>(healthDtos);
        return Result.success(info);
    }

    @PostMapping("/{page}/{size}")
    @ApiOperation("健康信息查询")
    public Result postQuery(@PathVariable("page") int page, @PathVariable("size") int size, @RequestBody DealHealth dealHealth) {
        PageHelper.startPage(page, size);
        List<HealthDto> healthDtos = healthService.postQuery(dealHealth);
        PageInfo<HealthDto> info = new PageInfo<>(healthDtos);
        return Result.success(info);
    }

    @PostMapping
    @ApiOperation("健康信息打卡")
    public Result postHealth(HttpServletRequest request, @RequestBody Health health) {
        Staff staff = jwtTokenUtil.getStaffFromRequest(request);
        health.setStaffId(staff.getStaffId());
        boolean save = healthService.save(health);
        if (save) return Result.success("体温数据提交成功");
        return Result.error("体温数据提交失败");
    }
}
